package com.j.lemon.learn.thread.interrapt;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author lijunjun
 * @Date 2019-04-02 10:19
 * @Description 最简单的控制方式，设置一个标志位，来停止当前动作
 */
public class Demo1 implements Runnable{
    private final List<BigInteger>  lists = new ArrayList<>();
    private volatile boolean isCancelled;
    @Override
    public void run() {
        BigInteger bigInteger = BigInteger.ONE;
        while(!isCancelled){
            bigInteger = bigInteger.nextProbablePrime();
            synchronized (this){
                lists.add(bigInteger);
            }
        }
    }

    void cancel(){
        isCancelled=true;
    }

    synchronized List<BigInteger> get(){
        return new ArrayList<>(lists);
    }
}

